package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.U;
import com.example.demo.service.UService;
import com.example.demo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
//@CrossOrigin(origins="*")
//@CrossOrigin(origins="http://localhost:7000/",methods={RequestMethod.GET, RequestMethod.POST,RequestMethod.OPTIONS},allowCredentials="true")
@Slf4j
@RestController
public class ULoginController {
    @Autowired
    UService uService;
    @PostMapping("/log")
    public Result login(@RequestBody U u){
        U e=uService.login(u);
        if(e!=null){
            Map<String, Object> Claims=new HashMap<>();
            Claims.put("id",e.getUserId());
            Claims.put("username",e.getUserName());
            String jwt = JwtUtils.generateJwt(Claims);
            e.setJwt(jwt);
            return Result.success(e);
        }

        return Result.error("用户名或密码错误");
    }
    /*@PostMapping("/register")
    @ApiOperation("注册")
    public Result register(@RequestBody User user){
        catService.register(user);
        System.out.println(user.getImg());
        return Result.success();
    }*/
    /*@DeleteMapping("/userdelete/{id}")
    public Result userDelete(@PathVariable Integer id){
        catService.userDelete(id);
        return Result.success();

    }
    @PostMapping("/adminlogin")
    @ApiOperation("管理员登录")
    public Result adminLogin(@RequestBody admin admin){
        admin e=catService.adminlogin(admin);
        if(e!=null){
            return Result.success(e);
        }
        return Result.error("管理员账号或密码错误");
    }*/
}
